package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeConfirmationDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeConfirmation;
import com.neusoft.neuiotms.dcom.repository.change.ChangeConfirmationRepository;

@Component
public class ChangeConfirmationConverter extends AbstractConverter<ChangeConfirmation, ChangeConfirmationDTO> {

    @Autowired
    private ChangeConfirmationRepository changeConfirmationRepository;

    @Override
    public ChangeConfirmation toModel(final ChangeConfirmationDTO dto) {
        ChangeConfirmation model;
        if (dto.isNew()) {
            model = new ChangeConfirmation();
        } else {
            model = this.changeConfirmationRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setUpdateTime(new Date());
        model.setOperation(dto.getOperation());
        model.setRemark(dto.getRemark());

        return model;
    }

    @Override
    public ChangeConfirmationDTO toDTO(final ChangeConfirmation model, final boolean forListView) {
        final ChangeConfirmationDTO dto = new ChangeConfirmationDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setOperation(model.getOperation());
        dto.setRemark(model.getRemark());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }
}
